package com.app.qbake.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.util.HashMap;

public class MediaUtil {

    public static void startVideoCapture(Activity activity, int requestCode){
       /* Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // 视频质量低0   高1
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 15);//传了也没用
        activity.startActivityForResult(intent, requestCode);*/



    }

    public static String getVideoPath(Context context, Uri uri){
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.moveToNext()) {
            //int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID));
            // 视频路径
            String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA));
            int size = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns.SIZE));

            // ThumbnailUtils类2.2以上可用  Todo 获取视频缩略图
            //Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MICRO_KIND);
            cursor.close();

            return filePath;
        }

        return "";
    }

    public static Bitmap getVideoFirstFrame(String videoPath){
        Bitmap bitmap = null;
        //MediaMetadataRetriever 是android中定义好的一个类，提供了统一
        //的接口，用于从输入的媒体文件中取得帧和元数据；
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            if (videoPath.startsWith("http")) {
                //根据文件路径获取缩略图
                retriever.setDataSource(videoPath, new HashMap());
            }else {
                retriever.setDataSource(videoPath);
            }
            //获得第一帧图片
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            retriever.release();
        }
        return bitmap;

    }

    public static void startVideoPlayer(Activity activity, String videoPath) {
        Intent intent=new Intent(Intent.ACTION_VIEW);
        if (videoPath.startsWith("http")){
            String extension=MimeTypeMap.getFileExtensionFromUrl(videoPath);
            String mimeType=MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            intent.setDataAndType(Uri.parse(videoPath),mimeType);
        }else {
            Uri uri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                uri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".FileProvider", new File(videoPath));

            }else {
                uri=Uri.fromFile(new File(videoPath));
            }
            intent.setDataAndType(uri,"video/*");
        }
        activity.startActivity(intent);

    }
}
